package com.ksyun.campus.dataserver.config.dataserverconfig;

import lombok.Data;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.PostConstruct;
import java.io.File;

/**
 * @author sxt
 * @version 1.0
 * @description TODO
 * @date 2023/8/9 21:24
 */
@Data
//@ConfigurationProperties(prefix = "az")
public class DataServerConfigProperties {

    /**
     * 容量类型
     */
    enum CAP_TYPE {
        GB("GB", 0);

        CAP_TYPE(String type, Integer code) {
            this.type = type;
            this.code = code;
        }

        private String type;

        private Integer code;

        public String getType() {
            return type;
        }

        public Integer getCode() {
            return code;
        }
    }
    @Value("${az.capacity}")
    private int capacity;

    /**
     * 机架
     */
    @Value("${az.rack}")
    private String rack;

    /**
     * 区域：北京、上海、天津
     */
    @Value("${az.zone}")
    private String zone;

    private CAP_TYPE capType = CAP_TYPE.GB;

    @Value("${zookeeper.root-name}")
    private String rootName;

    @Value("${zookeeper.node-name}")
    private String nodeName;

    /**
     * 数据保存根路径:
     * Windows： C:\Users\登录用户~1\AppData\Local\Temp\dataserver\node1
     * Linux： /tmp/dataserver/node1
     */
    private String dataPath = System.getProperty("java.io.tmpdir")+rootName+nodeName;

    /**
     * 若未配置数据保存路径，则指定一个默认路径
     */
    @PostConstruct
    private void dealDefaultDataPath() {
        if (Strings.isEmpty(dataPath)) {
            dataPath = System.getProperty("user.home") + File.separator + "/data-server/" + File.separator + nodeName;
        }
    }

    public int getCapacity() {
        if (capType.equals(CAP_TYPE.GB)){
            return capacity * 1024 * 1024 * 1024;
        }else {
            return capacity;
        }
    }

    public String getFullNode() {
        return rootName + nodeName;
    }
}
